package com.yj.web.dao.mapper;

import com.yj.web.dao.po.admin.Agreement;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by yanlinyang on 2017/7/24.
 */
@Component
public interface AgreementMapper {
    @Select("select * from cus_agreement where deleted_flag = 'N' and type = #{type} order by created_at desc limit #{size} offset #{offset}")
    public List<Agreement> listAgreement(@Param("type") int type, @Param("offset") int offset, @Param("size") int size);

    @Select("select count(*) from cus_agreement where deleted_flag = 'N' and type = #{type}")
    public int countAgreement(@Param("type") int type);

    @Select("select count(*) from cus_agreement where deleted_flag = 'N' and type = #{type} and status=1")
    public int countAgreementActive(@Param("type") int type);

    @Delete("update cus_agreement set deleted_flag = 'Y' where id = #{id} and deleted_flag = 'N' and status = 0")
    public int deleteAgreementById(@Param("id") long id);

    @Insert("insert into cus_agreement (create_id, created_at, content, title, link_url, type, status) " +
            "values (#{agreement.create_id}, now(), #{agreement.content}, #{agreement.title}, #{agreement.link_url}, #{agreement.type}," +
            "#{agreement.status})")
    @Options(useGeneratedKeys = true, keyProperty = "agreement.id")
    public long insertAgreement(@Param("agreement") Agreement agreement);

    @Update("update cus_agreement set update_id=#{agreement.update_id}, updated_at=now(), content=#{agreement.content}, title=#{agreement.title}, " +
            "link_url=#{agreement.link_url}, status=#{agreement.status} where id = #{agreement.id} and deleted_flag = 'N' and type=#{agreement.type}")
    public boolean updateAgreement(@Param("agreement") Agreement agreement);

    @Select("select * from cus_agreement where id = #{id} and deleted_flag = 'N'")
    public Agreement findAgreementById(@Param("id") long id);

    @Update("update cus_agreement set status=#{status} where id = #{id}")
    public boolean updateAgreementStatus(@Param("id") int id, @Param("status") int status);
    @Select("select id from cus_agreement where status = 1 and deleted_flag = 'N' and type = #{type}")
    public  int getActiveId(@Param("type") int type);
}
